#include "../FIFO/ringBuff.h"

#define  FLASE   1 
#define  TRUE    0 
#define  RINGBUFF_LEN  		1024  	//定义最大接收字节数 200


typedef struct
{
    u16 Head;           
    u16 Tail;
    u16 Lenght;
    u8 Ring_Buff[RINGBUFF_LEN];
}RingBuff_t;

//uint8_t   UART_RX_BUF[1024]   __attribute__ ((at(0X20001000)));   //就是将串口接收的数据定位到RAM中起始地址为0X20001000；
RingBuff_t ringBuff  __attribute__ ((at(0X20000100)));
//static RingBuff_t ringBuff;//创建一个ringBuff的缓冲区
void RingBuff_Init(void);
u8 Write_RingBuff(u8 data);
u8 Read_RingBuff(u8 **rData,u16 *len);
FIFO_Type FIFO={RingBuff_Init,Write_RingBuff,Read_RingBuff};

void RingBuff_Init(void)
{
    ringBuff.Head = 0;
    ringBuff.Tail = 0;
    ringBuff.Lenght = 0;
}

u8 Write_RingBuff(u8 data)
{
    if(ringBuff.Lenght >= RINGBUFF_LEN) //判断缓冲区是否已满
    {
       return FLASE; 
    }
     ringBuff.Ring_Buff[ringBuff.Tail]=data;
     ringBuff.Tail = (ringBuff.Tail+1)%RINGBUFF_LEN;//防止越界非法访问
     ringBuff.Lenght++;
     return TRUE; 
}

//u8 Read_RingBuff(u8 *rData)
//{
//    if(ringBuff.Lenght == 0)//判断非空
//    {
//        return FLASE; 
//    }
//    *rData = ringBuff.Ring_Buff[ringBuff.Head];//先进先出FIFO，从缓冲区头出
//    ringBuff.Head = (ringBuff.Head+1)%RINGBUFF_LEN;//防止越界非法访问
//    ringBuff.Lenght--;
//    return TRUE; 
//}

u8 Read_RingBuff(u8 **rData,u16 *len)
{
    if(ringBuff.Lenght == 0)//判断非空
    {
        return FLASE; 
    }
		*rData = &ringBuff.Ring_Buff[ringBuff.Head];
		*len = ringBuff.Lenght;
    RingBuff_Init();
    return TRUE; 
}
